﻿using System;
using System.Collections.Generic;
using System.Linq;
using GCTrackableRace.Interfaces;
using GcTrackableRace.Entities;

namespace GcTrackableRace.ScoreCalculator.Rules
{
	public class UniqueCacherDiscoveriesMadeRule: BaseRule, IRaceRule
	{
		public UniqueCacherDiscoveriesMadeRule(string reportColumnName, string startDate, string endDate, string pointAllocation)
			:base(reportColumnName, startDate, endDate, pointAllocation)
		{
		}

		protected override void SetRuleName()
		{
			RuleName = "Unique cacher discoveries made";
		}

		public override void Process(ITrackable trackable, IEnumerable<ITrackableLog> trackableLogs)
		{
			var cacherTeamName = (
			       	from log in trackableLogs
			       	where (
			       	      	log.LogType == LogType.DiscoveredIt
			       	      	&&
			       	      	log.Date >= StartDate
			       	      	&&
			       	      	log.Date <= EndDate
			       	      )
			       	orderby (log.ByCacher)
			       	select log)
				.Select(o => o.ByCacher)
				.Distinct()
				.ToList();

			trackable.AddScore(new RuleScore(
				RuleName, 
				ReportColumnName,
				cacherTeamName.Count,
				(int)(cacherTeamName.Count * PointAllocation),
				String.Join("|", cacherTeamName).Trim('|')));
		}
	}
}